package cn.iocoder.yudao.module.pickbook.controller.admin.bookcontent;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.pickbook.controller.admin.bookcontent.vo.*;
import cn.iocoder.yudao.module.pickbook.dal.dataobject.bookcontent.BookContentDO;
import cn.iocoder.yudao.module.pickbook.service.bookcontent.BookContentService;

@Tag(name = "管理后台 - 绘本内容")
@RestController
@RequestMapping("/pickbook/book-content")
@Validated
public class BookContentController {

    @Resource
    private BookContentService bookContentService;

    @PostMapping("/create")
    @Operation(summary = "创建绘本内容")
    @PreAuthorize("@ss.hasPermission('pickbook:book-content:create')")
    public CommonResult<Long> createBookContent(@Valid @RequestBody BookContentSaveReqVO createReqVO) {
        return success(bookContentService.createBookContent(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新绘本内容")
    @PreAuthorize("@ss.hasPermission('pickbook:book-content:update')")
    public CommonResult<Boolean> updateBookContent(@Valid @RequestBody BookContentSaveReqVO updateReqVO) {
        bookContentService.updateBookContent(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除绘本内容")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pickbook:book-content:delete')")
    public CommonResult<Boolean> deleteBookContent(@RequestParam("id") Long id) {
        bookContentService.deleteBookContent(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除绘本内容")
                @PreAuthorize("@ss.hasPermission('pickbook:book-content:delete')")
    public CommonResult<Boolean> deleteBookContentList(@RequestParam("ids") List<Long> ids) {
        bookContentService.deleteBookContentListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得绘本内容")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pickbook:book-content:query')")
    public CommonResult<BookContentRespVO> getBookContent(@RequestParam("id") Long id) {
        BookContentDO bookContent = bookContentService.getBookContent(id);
        return success(BeanUtils.toBean(bookContent, BookContentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得绘本内容分页")
    @PreAuthorize("@ss.hasPermission('pickbook:book-content:query')")
    public CommonResult<PageResult<BookContentRespVO>> getBookContentPage(@Valid BookContentPageReqVO pageReqVO) {
        PageResult<BookContentDO> pageResult = bookContentService.getBookContentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BookContentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出绘本内容 Excel")
    @PreAuthorize("@ss.hasPermission('pickbook:book-content:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBookContentExcel(@Valid BookContentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BookContentDO> list = bookContentService.getBookContentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "绘本内容.xls", "数据", BookContentRespVO.class,
                        BeanUtils.toBean(list, BookContentRespVO.class));
    }

}